package archer.framework.common.support;

import archer.framework.protocol.result.ExecuteResultWrapper;
import archer.framework.security.apitenant.ApiTenant;
import archer.framework.security.utils.ApiTenantContext;
import archer.framework.utils.ValidateUtils;
import jodd.json.JsonSerializer;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotWritableException;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

/**
 * @author christ
 * @date 2016/6/2
 */
public class JoddJsonHttpMessageConverterForApiTenant extends archer.framework.web.extension.jodd.JoddJsonHttpMessageConverter {

    /**
     * 对象转json
     *
     * @param o
     * @param outputMessage
     * @throws IOException
     * @throws HttpMessageNotWritableException
     */
    protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {

        OutputStream out = outputMessage.getBody();

        JsonSerializer serializer = new JsonSerializer().deep(true);

        if (o instanceof ExecuteResultWrapper) {

            ExecuteResultWrapper wrapper = getExecuteResultWrapper(o, ApiTenantContext.getApiTenant());

            // 仅针对外部接口用户进行默认排除
            if (ValidateUtils.isNotEmpty(defaultExcludes) && ApiTenant.TYPE_EXTERNAL.equalsIgnoreCase(ApiTenantContext.getType())) {
                for (String each : defaultExcludes) {
                    serializer.exclude(each);
                }
            }

            List<String> excludes = wrapper.getExcludes();
            if (ValidateUtils.isNotEmpty(excludes)) {
                for (String each : excludes) {
                    serializer.exclude(each);
                }
            }

            // 添加
            List<String> includes = wrapper.getIncludes();
            if (ValidateUtils.isNotEmpty(includes)) {
                for (String each : includes) {
                    serializer.include(each);
                }
            }

            // 序列化被代理的ExecuteResult
            String s = serializer.serialize(wrapper.getExecuteResult());
            if (ValidateUtils.isNotEmpty(s)) {
                out.write(s.getBytes(charset));
            }
            return;
        }

        String s = serializer.serialize(o);
        if (ValidateUtils.isNotEmpty(s)) {
            out.write(s.getBytes(charset));
        }
    }

    /**
     * 获得结果包装类
     *
     * @param wrapper
     * @param apiTenant
     * @return
     */
    protected ExecuteResultWrapper getExecuteResultWrapper(Object wrapper, ApiTenant apiTenant) {

        return (ExecuteResultWrapper) wrapper;
    }

    /**
     * 默认的过滤字段
     */
    private List<String> defaultExcludes = Arrays.asList("resultData.*.creator", "resultData.*.modifier", "resultData.*.createTime", "resultData.*.deleted", "resultData.*.modifyTime", "resultData.*.creatorSystem", "resultData.*.modifierSystem");

    public List<String> getDefaultExcludes() {
        return defaultExcludes;
    }

    public JoddJsonHttpMessageConverterForApiTenant setDefaultExcludes(List<String> defaultExcludes) {
        this.defaultExcludes = defaultExcludes;
        return this;
    }
}
